﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using WeiXinPF.Model;
using WeiXinPF.Modules.Core.Admin.Domain.Models;

namespace WeiXinPF.Modules.Core.Admin.Infrastructure.Repositories
{
    public class AdvertisementRepositories : IAdvertisementRepositories
    {
        private readonly SmartContext _context;
        public AdvertisementRepositories(SmartContext context)
        {
            _context = context;
        }
        public void Add(advertisement entity)
        {
            _context.advertisement.Add(entity);
        }

        public void Modify(advertisement entity)
        {
            _context.advertisement.Attach(entity);
            _context.Entry(entity).State = EntityState.Modified;
        }

        public void Delete(int entityId)
        {
            var entity = Get(entityId);
            _context.advertisement.Remove(entity);
        }

        public void Delete(Expression<Func<advertisement, bool>> predicate)
        {
            var systementitys = GetAllList(predicate);
            _context.advertisement.RemoveRange(systementitys);
        }

        public IQueryable<advertisement> GetAllList(Expression<Func<advertisement, bool>> predicate)
        {
            return _context.advertisement.Where(predicate);
        }

        public IQueryable<advertisement> GetAllList()
        {
            return _context.advertisement;
        }

        public advertisement Get(int id)
        {
            return _context.advertisement.SingleOrDefault(b => b.id == id);
        }

        public void Delete(advertisement entity)
        {
            _context.advertisement.Remove(entity);
        }

        public void DeleteList(List<advertisement> list)
        {
            _context.advertisement.RemoveRange(list);
        }

        public void SaveChange()
        {
            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        Trace.TraceInformation("Property: {0} Error: {1}",
                                                validationError.PropertyName,
                                                validationError.ErrorMessage);
                    }
                }
            }

        }

        public void Modify(IQueryable<advertisement> Messages)
        {
            Messages.ToList().ForEach(c =>
            {
                _context.advertisement.Attach(c);
                _context.Entry(c).State = EntityState.Modified;
            });
        }
    }
}
